/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.<br>
 */
package com.ce.pms.system.renovation.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.ce.common.util.BlankUtil;
import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.common.dao.IOrganizationalStructureDao;
import com.ce.pms.common.model.HmEmployeeDomain;
import com.ce.pms.common.vo.OrganizationalStructureVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.renovation.dao.IApprovalItemConfigDao;
import com.ce.pms.system.renovation.model.ApprovalItemConfigDO;
import com.ce.pms.system.renovation.service.IApprovalItemConfigService;
import com.ce.pms.system.renovation.vo.ApprovalItemConfigVO;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：装修审核项目<br>
 * @author 易贤建 创建日期：2008-12-25<br>
 */
public class ApprovalItemConfigServiceImpl extends PmsBaseServiceImpl implements IApprovalItemConfigService {
	/**
	 * 装修项目DAO
	 */
	IApprovalItemConfigDao approvalItemConfigDao = null;
	/**
	 * 组织架构DAO
	 */
	IOrganizationalStructureDao organizationalStructureDao = null;
	/**
	 * 功能描述:通过ID获取装修申请项目<br>
	 * @param appDOs
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-29  <br>
	 */
	public ApprovalItemConfigVO getApprovalItemConfigById(Long keyword){
		super.checkParameterIsNull(keyword, "system:approvalItemConfig_ID_NULL");
		ApprovalItemConfigDO appDO = approvalItemConfigDao.findApprovalItemConfigByID(keyword);
		if(appDO == null)
			throw new BusinessException("system:approvalItemConfig_DO_NOT_FOUND",BusinessException.ERROR);
		return assembleD2V(appDO);
	}
	/**
	 * 功能描述:验证项目名称不能一样--没有ID的<br>
	 * @param keyword
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-30  <br>
	 */
	public void validationApprovalItemConfigItemName(ApprovalItemConfigVO approvalItemConfigVO){
		if(approvalItemConfigVO == null) return ;
		if(BlankUtil.isBlank(approvalItemConfigVO.getItemName())) return ;

		Long keyword = approvalItemConfigVO.getKeyword();
		String itemName = approvalItemConfigVO.getItemName();

		List<ApprovalItemConfigDO> appDOS = null;
		if(keyword == null){
			appDOS = approvalItemConfigDao.findApprovalItemConfigByItemName(itemName);
		}else{
			appDOS = approvalItemConfigDao.findByItemNameWithoutKeyword(keyword,itemName);
		}
		if(!BlankUtil.isBlank(appDOS))
			throw new BusinessException("system:approval_itemName_is_exsit",BusinessException.WARNING);
	}
	/**
	 * 功能描述:查询装修申请项目分页列表<br>
	 * @param appDOs
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-29  <br>
	 */
	@SuppressWarnings("unchecked")
	public Page queryApprovalItemList(SearchVO searchVO) {
		Page page =  approvalItemConfigDao.queryApprovalItemList(searchVO);
		List<ApprovalItemConfigDO> result = page.getResult();
		page.setResult(assembleD2V(result));
		return page;
	}
	/**
	 * 功能描述:添加装修审核项目<br>
	 * @param appDOs
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-29  <br>
	 */
	public ApprovalItemConfigVO add(ApprovalItemConfigVO approvalItemConfigVO){
		checkParameterIsNull(approvalItemConfigVO, "system:approval_info_not_null");//VO不能为空
		checkParameterIsNull(approvalItemConfigVO.getEmployeeIds(),"system:approvalItem_info_select_user"); //选择的员工不能为空
		checkParameterIsNull(approvalItemConfigVO.getItemName(),"system:approvalItem_info_item_not_null"); //审核项目名称不能为空
		validationApprovalItemConfigItemName(approvalItemConfigVO); //验证项目名称是否已经存在

		ApprovalItemConfigDO appDO = assembleV2D(approvalItemConfigVO);
		appDO.setSysInfo(this.createInfo(approvalItemConfigVO.getCreator()));
		appDO.setState(ApprovalItemConfigDO.STATE_OO1); //设置状态新建
		ApprovalItemConfigDO newDO = (ApprovalItemConfigDO)approvalItemConfigDao.saveObject(appDO);
		return assembleD2V(newDO);
	}
	/**
	 * 功能描述:修改装修审核项目<br>
	 * 1>从数据库查询对应ID的DO
	 * 2>设置审核项目名
	 * 3>设置选择的用户
	 * @param appDOs
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-29  <br>
	 */
	public ApprovalItemConfigVO edit(ApprovalItemConfigVO approvalItemConfigVO){
		checkParameterIsNull(approvalItemConfigVO, "system:approval_info_not_null");//VO不能为空
		checkParameterIsNull(approvalItemConfigVO.getKeyword(),"system:approval_info_id_not_null");//ID不能为空
		checkParameterIsNull(approvalItemConfigVO.getEmployeeIds(),"system:approvalItem_info_select_user"); //选择的员工不能为空
		checkParameterIsNull(approvalItemConfigVO.getItemName(),"system:approvalItem_info_item_not_null"); //审核项目名称不能为空

		ApprovalItemConfigDO appDO = approvalItemConfigDao.findApprovalItemConfigByID(approvalItemConfigVO.getKeyword());
		if(appDO == null)
			throw new BusinessException("system:approval_info_object_not_found",BusinessException.FAILURE);

		appDO.setItemName(approvalItemConfigVO.getItemName());
		processV2DForEmployee(approvalItemConfigVO,appDO); //处理用户
		appDO.setSysInfo(this.createInfo(approvalItemConfigVO.getModifier()));

		return  assembleD2V(appDO);
	}
	public void delete(Long keyword){
		checkParameterIsNull(keyword,"system:approval_info_id_not_null");//ID不能为空
		ApprovalItemConfigDO appDO = approvalItemConfigDao.findApprovalItemConfigByID(keyword);
		if(appDO == null)
			throw new BusinessException("system:approval_info_object_not_found",BusinessException.FAILURE);
		approvalItemConfigDao.doDelete(appDO);
	}
	/**
	 * 功能描述:DO列表转换为VO列表<br>
	 * @param appDOs
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-29  <br>
	 */
	private List<ApprovalItemConfigVO> assembleD2V(final List<ApprovalItemConfigDO> appDOs){
		if(BlankUtil.isBlank(appDOs)) return null;
		List<ApprovalItemConfigVO> appVOs = new ArrayList<ApprovalItemConfigVO>();
		for(ApprovalItemConfigDO appDO : appDOs){
			appVOs.add(assembleD2V(appDO));
		}
		return appVOs;
	}
	/**
	 * 功能描述:VO转DO<br>
	 * @param approvalItemConfigVO
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-29  <br>
	 */
	private ApprovalItemConfigDO  assembleV2D(ApprovalItemConfigVO approvalItemConfigVO){
		if(approvalItemConfigVO == null) return null;

		ApprovalItemConfigDO approvalItemDO = new ApprovalItemConfigDO();
		approvalItemDO.setKeyword(approvalItemConfigVO.getKeyword());
		approvalItemDO.setItemName(approvalItemConfigVO.getItemName()); //处理选择的员工列表
		processV2DForEmployee(approvalItemConfigVO,approvalItemDO);
		return approvalItemDO;
	}
	/**
	 * 功能描述:处理员工列表<br>
	 * 先删除所有已有的 然后添加VO中所有的
	 * @param appVO
	 * @param appDO
	 * Create author:yixianjian <br>
	 * Create on:2008-12-29  <br>
	 */
	private void processV2DForEmployee(ApprovalItemConfigVO appVO,ApprovalItemConfigDO appDO){
		List<HmEmployeeDomain> list = organizationalStructureDao.queryEmployeeByIDs(appVO.getEmployeeIds());
		Set<HmEmployeeDomain> employeeSet = new HashSet<HmEmployeeDomain>();
		employeeSet.addAll(list);
		appDO.setApprovallers(employeeSet);
	}

	/**
	 * 功能描述: DO转VO<br>
	 * @param approvalItemConfigDO
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-29  <br>
	 */
	private ApprovalItemConfigVO assembleD2V(ApprovalItemConfigDO approvalItemConfigDO){
		if(approvalItemConfigDO == null) return null;

		ApprovalItemConfigVO approvalitemConfigVO = new ApprovalItemConfigVO();
		approvalitemConfigVO.setKeyword(approvalItemConfigDO.getKeyword()); //keyword
		approvalitemConfigVO.setItemName(approvalItemConfigDO.getItemName()); //项目名称
		approvalitemConfigVO.setEmployeName(processEmployeeName(approvalItemConfigDO)); //员工姓名
		//设置员工DO 转换为VO
		approvalitemConfigVO.setApprovallers(processEmployeeDomain2V(approvalItemConfigDO.getApprovallers()));
		approvalitemConfigVO.setState(approvalItemConfigDO.getState()); //状态
		return approvalitemConfigVO;
	}
	/**
	 * 功能描述: 将员工Domain转换为员工VO,支持集合级别的<br>
	 * @param employeeSet
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-30  <br>
	 */
	private Set<OrganizationalStructureVO> processEmployeeDomain2V(Set<HmEmployeeDomain> employeeDomainSet){
		if(com.ce.util.BlankUtil.isBlank(employeeDomainSet)) return null;

		Set<OrganizationalStructureVO> employeeVOSet = new HashSet<OrganizationalStructureVO>();
		for(HmEmployeeDomain domain : employeeDomainSet){
			employeeVOSet.add(assembleD2V(domain));
		}
		return employeeVOSet;
	}
	/**
	 * 功能描述:将DO转VO<br>
	 * @param emloyeeDomain
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-30  <br>
	 */
	private OrganizationalStructureVO assembleD2V(HmEmployeeDomain emloyeeDomain){
		if(emloyeeDomain == null) return null;
		OrganizationalStructureVO vo = new OrganizationalStructureVO();
		vo.setEmplyeeId(emloyeeDomain.getEmployeeId());
		vo.setEmployeeName(emloyeeDomain.getEmployeeName());
		return vo;
	}
	/**
	 * 功能描述:将DO里面得员工SET转换为VO<br>
	 * @param approvalItemConfig
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-29  <br>
	 */
	private String processEmployeeName(ApprovalItemConfigDO approvalItemConfig){
		StringBuffer empployeeName = new StringBuffer();
		Iterator<HmEmployeeDomain> iterator = approvalItemConfig.getApprovallers().iterator();
		if(iterator.hasNext()){
			empployeeName.append(iterator.next().getEmployeeName());
		}
		while(iterator.hasNext()){
			empployeeName.append(",").append(iterator.next().getEmployeeName());
		}
		return empployeeName.toString();
	}

	public void setApprovalItemConfigDao(
			IApprovalItemConfigDao approvalItemConfigDao) {
		this.approvalItemConfigDao = approvalItemConfigDao;
	}

	public void setOrganizationalStructureDao(
			IOrganizationalStructureDao organizationalStructureDao) {
		this.organizationalStructureDao = organizationalStructureDao;
	}
}
